package com.sec.etech.sys.model;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class DelayTask implements Delayed {

    private String type;
    private JSONObject data;
    private long expire;  //ms

    public DelayTask(String type, JSONObject data, long expire){
        super();
        this.type = type;
        this.data = data;
        this.expire = expire + System.currentTimeMillis();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis(), unit);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int)(getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS));
    }
}
